package com.lixueju.security.box.web.authentication;

import com.lixueju.security.box.web.authentication.filter.SmsSecurityBoxAuthenticationFilter;
import com.lixueju.security.box.web.authentication.filter.UsernamePasswordSecurityBoxAuthenticationFilter;
import com.lixueju.security.box.web.authentication.filter.WeixinDecodeSecurityBoxAuthenticationFilter;
import com.lixueju.security.box.web.authentication.filter.WeixinSecurityBoxAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * @author lixueju
 * @since 2019/11/12 18:01
 **/
@Component
public class SecurityBoxAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
    @Autowired
    private AuthenticationSuccessHandler lixuejuAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler lixuejuAuthenticationFailureHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(HttpSecurity http) throws Exception {

        // 用户名密码
        UsernamePasswordSecurityBoxAuthenticationFilter usernamePasswordSecurityBoxAuthenticationFilter = new UsernamePasswordSecurityBoxAuthenticationFilter();
        usernamePasswordSecurityBoxAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        usernamePasswordSecurityBoxAuthenticationFilter.setAuthenticationSuccessHandler(lixuejuAuthenticationSuccessHandler);
        usernamePasswordSecurityBoxAuthenticationFilter.setAuthenticationFailureHandler(lixuejuAuthenticationFailureHandler);

        // 短信验证码
        SmsSecurityBoxAuthenticationFilter smsSecurityBoxAuthenticationFilter = new SmsSecurityBoxAuthenticationFilter();
        smsSecurityBoxAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        smsSecurityBoxAuthenticationFilter.setAuthenticationSuccessHandler(lixuejuAuthenticationSuccessHandler);
        smsSecurityBoxAuthenticationFilter.setAuthenticationFailureHandler(lixuejuAuthenticationFailureHandler);

        // 微信
        WeixinSecurityBoxAuthenticationFilter weixinSecurityBoxAuthenticationFilter = new WeixinSecurityBoxAuthenticationFilter();
        weixinSecurityBoxAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        weixinSecurityBoxAuthenticationFilter.setAuthenticationSuccessHandler(lixuejuAuthenticationSuccessHandler);
        weixinSecurityBoxAuthenticationFilter.setAuthenticationFailureHandler(lixuejuAuthenticationFailureHandler);

        // 微信解密
        WeixinDecodeSecurityBoxAuthenticationFilter weixinDecodeSecurityBoxAuthenticationFilter = new WeixinDecodeSecurityBoxAuthenticationFilter();
        weixinDecodeSecurityBoxAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        weixinDecodeSecurityBoxAuthenticationFilter.setAuthenticationSuccessHandler(lixuejuAuthenticationSuccessHandler);
        weixinDecodeSecurityBoxAuthenticationFilter.setAuthenticationFailureHandler(lixuejuAuthenticationFailureHandler);

        SecurityBoxAuthenticationProvider commonAuthenticationProvider = new SecurityBoxAuthenticationProvider();
        commonAuthenticationProvider.setUserDetailsService(userDetailsService);

        http.authenticationProvider(commonAuthenticationProvider)
                .addFilterAfter(usernamePasswordSecurityBoxAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(smsSecurityBoxAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(weixinSecurityBoxAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(weixinDecodeSecurityBoxAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

    }
}
